
import Result from "../vo/Result";
import {baseUrlAdmin, baseUrlAdminLogin} from "./config";
import {getLocalUserInServer} from "./utils/userUtils";


//需要权限的 apiUrl
const urls=[

      ]

//页面请求 拦截所有 baseUrlAdmin开头的 api拦截上面的url
export  function serverAuthCheck(req, res, next,adminUserService) {
  let token = getLocalUserInServer(req);
  let user= adminUserService.login(token);
  let path=req.path;
  //先拦截页面
  let isAdmin = path.startsWith(baseUrlAdmin)
  if(isAdmin){
    //没有登录 跳转到登录
    if (!user && path != baseUrlAdminLogin) {
      return res.redirect(baseUrlAdminLogin);
    }
    //已经登录还在 直接跳转到首页
    else if (user && path == baseUrlAdminLogin) {
      return res.redirect(baseUrlAdmin);
    }
  }
  //再拦截api请求
  if (urls.includes(path) && !user) {
    return Result.errCode(res,401, "请先登录后操作！");
  } else {
    return next();
  }
}

